'use strict';
app.controller('payHistoryCtrl',[ '$scope','utilService','$modal','payHistoryService','toaster','$filter',function($scope,utilService,$modal,payHistoryService,toaster,$filter) {

    $scope.payHistory={};
    $scope.payHistory.startTime=new Date().getFullYear()+"-"+(new Date().getMonth()+1)+"-01";
    $scope.payHistory.endTime=$filter('date')(new Date(),'yyyy-MM-dd');
    $scope.convert = function(){
        if($scope.payHistory.startTime!=null){
            $scope.payHistory.startTime = $filter("date")($scope.payHistory.startTime, "yyyy-MM-dd");
        }
        if($scope.payHistory.endTime!=null){
            $scope.payHistory.endTime = $filter("date")($scope.payHistory.endTime, "yyyy-MM-dd");
        }
    };

    $scope.query=function(){
        $scope.convert();
        utilService.queryForPagingList($scope,{
            url:$scope.app.domain+"pay/history",
            params: $scope.payHistory
        }).then(function(result){
            console.log(result);
        })
    };

    $scope.query();

    $scope.openExportDate = function(){
        openModal("确定要导出已查询出的记录吗？" +
            "(导出时间跟导出数据量的大小有关，请耐心等待)");
        function openModal(msg){
            utilService.modal($modal, $scope, {
                title:"温馨提示",
                controller:"exportDataCtrl",
                message:msg,
                data: $scope.payHistory
            });
        }
    }

    //查询连锁品牌下店铺列表
    $scope.getShopList = function () {
        payHistoryService.getShopList($scope).then(
            function (rlt) {
                switch (rlt.status) {
                    case 200:
                        $scope.shopList = rlt.data;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            }
        );
    };
    $scope.getShopList();
}]);

app.controller('exportDataCtrl', ['$scope', '$modalInstance','$state','payHistoryService','toaster','modalData',
    function($scope, $modalInstance,$state,payHistoryService,toaster,modalData) {
        $scope.modal = modalData;
        $scope.param = modalData.data;
        $scope.AllowToDelete=true;

        $scope.ok=function(){
            if(!$scope.AllowToDelete){
                return;
            }
            $scope.AllowToDelete=false;
            payHistoryService.exportData($scope);
            $modalInstance.dismiss('success');
        }
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);